# Copyright 2020 Oracle Corporation and/or affiliates.  All rights reserved.
# Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl

resource "null_resource" "sdb_shard_relay_tns_config" {
  depends_on = ["null_resource.sdb_shard_director_configure", "null_resource.sdb_shard_env_configure", "null_resource.sdb_shard_standby_configure", "null_resource.sdb_catalog_standby_configure", "null_resource.sdb_shard_director_install_main"]
  count      = var.num_of_shards

  triggers = {
    shard_db_ids              = "${join(",", oci_database_db_system.shard_db.*.id)}"
    shard_stdby_db_ips        = "${join(",", data.oci_core_vnic.shard_stby_db_node_vnic.*.public_ip_address)}"
    shard_catalog_standby_ips = "${join(",", data.oci_core_vnic.catalog_stby_db_node_vnic.*.public_ip_address)}"
  }

  #creates ssh connection to gsm host
  connection {
    type        = "ssh"
    user        = "${var.os_user}"
    private_key = "${tls_private_key.public_private_key_pair.private_key_pem}"
    host        = "${data.oci_core_vnic.shard_db_node_vnic[count.index].public_ip_address}"
    agent       = false
    timeout     = "${var.ssh_timeout}"
  }

  provisioner "file" {
    content     = <<-EOF
      #! /bin/bash
      source ${local.db_home_path}/shard.sh
      cp $TNS_ADMIN/tnsnames.ora $TNS_ADMIN/tnsnames-ora-bkup-"${timestamp()}"
      cp $TNS_ADMIN/tnsnames.ora $ORACLE_HOME/tns_sh${count.index}.tnscfg
      cd ${var.oracle_base}
      echo "${tls_private_key.public_private_key_pair.private_key_pem}" > ikey-relay
      chmod 600 ikey-relay
      scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ikey-relay $ORACLE_HOME/tns_sh${count.index}.tnscfg oracle@${oci_core_instance.gsm_vm[0].public_ip}:${var.oracle_base}/ 
      rm -f ikey-relay
      EOF
    destination = "${local.db_home_path}/shard_relay_tns_config.sh"
  }

  provisioner "remote-exec" {
    inline = [
      "chmod 700 ${local.db_home_path}/shard_relay_tns_config.sh",
      "${local.db_home_path}/shard_relay_tns_config.sh",
      "rm -f ${local.db_home_path}/shard_relay_tns_config.sh"
    ]
  }
}